package com.cyandpos.pad.offline.model;

import android.content.ContentValues;
import android.database.Cursor;

import com.cyandpos.pad.offline.offlinedb.CursorUtil;
import com.cyandpos.pad.offline.offlinedb.Sqldb;
import com.cyandpos.pad.offline.upload.GoodOffline;
import com.cyandpos.pad.offline.upload.OrderPayitemOffline;
import com.cyandpos.pad.offline.upload.PersonOffline;
import com.cyandpos.pad.offline.upload.ShopEmployeeInfoOffline;
import com.cyandpos.pad.offline.upload.TicketOffline;
import com.cyandpos.pad.offline.upload.TicketPayItemOffline;
import com.cyandpos.pad.utils.MoneyUtils;

import java.util.ArrayList;
import java.util.List;

public class QueryOfflineUploadData {

    public static List<TicketOffline> QueryOfflineOrders(Sqldb sqldb ){
        List<TicketOffline> ticketList = new ArrayList<>();
        try {
            //查询有多少条脱机数据
            Cursor cursor1 = sqldb.find("select ORDERNO,SKTNO,JLBH,FDBH from POS_TJSC where BJ =0");
            if(cursor1 != null && cursor1.getCount() > 0){
                while (cursor1.moveToNext()) {
                    TicketOffline ticket = new TicketOffline();
                    ticket.setOrderNo(CursorUtil.getString(cursor1, "ORDERNO"));
                    ticket.setSktNo(CursorUtil.getString(cursor1, "SKTNO"));
                    ticket.setJlbh(CursorUtil.getInt(cursor1, "JLBH"));
                    ticket.setStoreCode(CursorUtil.getString(cursor1, "FDBH"));
                    ticket.setCheckCrm(true);
                    ticketList.add(ticket);
                }
                cursor1.close();
            }


            for(TicketOffline ticket : ticketList){
                //查询POS_ORDER
                Cursor cursor2 = sqldb.find("select ORDERNO,DDLY,JYLX,SKY,XSJE,OLDORDERNO," +
                        "ZLJE,JZRQ,DDGN,STATUS,KSSJ,JSSJ from POS_ORDER where ORDERNO = " +"'"+ticket.getOrderNo()
                        +"'"+" and SKTNO = "+"'"+ticket.getSktNo()+"'"+" AND STATUS = 1");
                if(cursor2 != null && cursor2.getCount() > 0){
                    while (cursor2.moveToNext()) {
                        ticket.setDdly(CursorUtil.getInt(cursor2, "DDLY"));
                        ticket.setJylx(CursorUtil.getInt(cursor2, "JYLX"));
                        ticket.setSkyid(CursorUtil.getInt(cursor2, "SKY"));
                        ticket.setSaleMoney(MoneyUtils.getMoneyInt(CursorUtil.getString(cursor2, "XSJE")));
                        ticket.setOldOrderNo(CursorUtil.getString(cursor2, "OLDORDERNO"));
                        ticket.setChangeMoney(MoneyUtils.getMoneyInt(CursorUtil.getString(cursor2, "ZLJE")));
                        ticket.setOldjzrq(CursorUtil.getString(cursor2, "JZRQ"));
                        ticket.setOrderFunction(CursorUtil.getInt(cursor2, "DDGN"));
                        ticket.setStatus(CursorUtil.getInt(cursor2, "STATUS"));
                        ticket.setKssj(CursorUtil.getString(cursor2,"KSSJ"));
                        ticket.setJssj(CursorUtil.getString(cursor2,"JSSJ"));
                    }
                    cursor2.close();
                }

               //查询商品信息
                queryGoodsInfo(sqldb,ticket);
                //查询收款方式
                queryPaysInfo(sqldb,ticket);
                //查询收款员信息
                queryCashierInfo(sqldb,ticket);
                //查询营业员信息
                queryShopEmployeeInfo(sqldb,ticket);

                //查询ORDER_ITEM_PAY
//                Cursor cursor3 = sqldb.find("select ORDERNO,SKTNO,JLBH,SKFS,TICKT_INX,INX,SKJE,SP_ID,GUID,DEPTID " +
//                        "from ORDER_ITEM_PAY where ORDERNO = " +ticket.getOrderNo()+" and SKTNO = "+ticket.getSktNo());
//                while (cursor3.moveToNext()) {
//                    TicketPayItemOffline payItem = new TicketPayItemOffline();
//                    payItem.setPayCode(CursorUtil.getInt(cursor3, "SKFS"));
//                    payItem.setPayAmount(MoneyUtils.getMoneyInt(CursorUtil.getString(cursor3, "SKJE")));
//                    payItem.setPayGuid(CursorUtil.getString(cursor3, "GUID"));
//
//                    OrderPayitemOffline orderPayItem =new OrderPayitemOffline();
//                    orderPayItem.setJlbh(ticket.getJlbh()+"");
//                    orderPayItem.setSktno(ticket.getSktNo());
//                    orderPayItem.setOrderNo(ticket.getOrderNo());
//                    orderPayItem.setPay_guid(payItem.getPayGuid());
//                    orderPayItem.setPay_type(payItem.getPayType()+"");
//                    orderPayItem.setPay_code(payItem.getPayCode()+"");
//                    orderPayItem.setPay_name(payItem.getPayName());
//                    orderPayItem.setPay_amount(MoneyUtils.getMoneyStr(payItem.getPayAmount()));
//                    payItem.getPayInfoList().add(orderPayItem);
//                    ticket.getPayList().add(payItem);
//                }
//                cursor3.close();



                //查询商品——ORDER_ITEM

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ticketList;
    }

    /**
     * 查询营业员
     * @param sqldb
     * @param ticket
     */
    private static void queryShopEmployeeInfo(Sqldb sqldb, TicketOffline ticket) {
        Cursor cursor1 = sqldb.find("select ORDERNO,SKTNO,JLBH,TCKT_INX,YYY,DEPTID from ORDER_PERSON where ORDERNO = " +"'"+ticket.getOrderNo()
                +"'"+" and SKTNO = "+"'"+ticket.getSktNo()+"'");

        if(cursor1 != null && cursor1.getCount() > 0){
            while(cursor1.moveToNext()){
                ShopEmployeeInfoOffline yyyInfo = new ShopEmployeeInfoOffline();
                yyyInfo.setPersonId(CursorUtil.getInt(cursor1, "YYY"));
                yyyInfo.setTicktInx(CursorUtil.getInt(cursor1, "TCKT_INX"));
                yyyInfo.setPersonDeptId(CursorUtil.getInt(cursor1, "DEPTID"));
                ticket.getSalerInfoList().add(yyyInfo);
            }
        }
        cursor1.close();
    }

    private static void queryCashierInfo(Sqldb sqldb, TicketOffline ticket) {
        Cursor cursor1 = sqldb.find("select R.PERSON_ID,R.RYDM,R.PERSON_NAME,R.WORK_TYPE,R.DEPTID,R.SHOPID,B.BMDM,B.DEPT_NAME " +
                "from RYXX R ,BM B where R.PERSON_ID = " +ticket.getSkyid()+" and R.DEPTID=B.DEPTID");

        if(cursor1 != null && cursor1.getCount() > 0){
            while(cursor1.moveToNext()){
                PersonOffline skyInfo = new PersonOffline();
                skyInfo.setPersonId(ticket.getSkyid());
                skyInfo.setPersonCode(CursorUtil.getString(cursor1, "RYDM"));
                skyInfo.setPersonName(CursorUtil.getString(cursor1, "PERSON_NAME"));
                skyInfo.setPersonDeptId(CursorUtil.getInt(cursor1, "DEPTID"));
                skyInfo.setShopid(CursorUtil.getInt(cursor1, "SHOPID"));
                skyInfo.setPersonDeptCode(CursorUtil.getString(cursor1, "BMDM"));
                skyInfo.setPersonDeptName(CursorUtil.getString(cursor1, "DEPT_NAME"));
                ticket.setSkyInfo(skyInfo);
            }
        }
        cursor1.close();
    }

    private static void queryPaysInfo(Sqldb sqldb, TicketOffline ticket) {
        Cursor cursor1 = sqldb.find("select ORDERNO,SKTNO,JLBH,SKFS,SKJE,YYJE,PAYGUID " +
                "from ORDER_PAY where ORDERNO = " +"'"+ticket.getOrderNo()+"'"+" and SKTNO = "+"'"+ticket.getSktNo()+"'");
        if(cursor1 != null && cursor1.getCount() > 0){
            while(cursor1.moveToNext()){
                TicketPayItemOffline payItem = new TicketPayItemOffline();
                payItem.setPayCode(CursorUtil.getInt(cursor1, "SKFS"));
                payItem.setPayAmount(MoneyUtils.getMoneyInt(CursorUtil.getString(cursor1, "SKJE")));
                payItem.setOverflowAmount(MoneyUtils.getMoneyInt(CursorUtil.getString(cursor1, "YYJE")));
                payItem.setPayGuid(CursorUtil.getString(cursor1, "PAYGUID"));

                //查询收款方式——SKFS
                Cursor cursor1_2 = sqldb.find("select A.CODE,A.NAME,A.TYPE,YHQID,A.ZLCLFS,A.BJ_YH " +
                        "from SKFS A where A.CODE in (select SKFSID from SKT_SKFS where SKTNO= "
                        +"'"+ticket.getSktNo()+"'"+") and A.CODE = " +payItem.getPayCode());
                if(cursor1_2 != null && cursor1_2.getCount() > 0){
                    while(cursor1_2.moveToNext()){
                        payItem.setPayName(CursorUtil.getString(cursor1_2, "NAME"));
                        payItem.setPayType(CursorUtil.getInt(cursor1_2, "TYPE"));
                        payItem.setYhqid(CursorUtil.getString(cursor1_2, "YHQID").equals("")?-1:Integer.parseInt(CursorUtil.getString(cursor1_2, "YHQID")));
                        payItem.setChangeType(CursorUtil.getInt(cursor1_2, "ZLCLFS"));
                        payItem.setYhbj(CursorUtil.getInt(cursor1_2, "BJ_YH"));
                    }
                }
                cursor1_2.close();

                //查询收款方式是否编码券——YHQDEF
                Cursor cursor1_3 = sqldb.find("select ISCODED from YHQDEF where YHQID = "+payItem.getYhqid());
                if(cursor1_3 != null && cursor1_3.getCount() > 0){
                    while(cursor1_3.moveToNext()){
                        payItem.setIscoded(CursorUtil.getInt(cursor1_3, "ISCODED"));
                    }
                }
                cursor1_3.close();

                //查询第三方明细表——ORDER_PAYMENT_DETAIL
//            (ORDERNO,INX,SKFS,SKTNO,JLBH,JYSJ,LX,JYTYPE,SHDDH,WBDDH,SKJE,YHJE,YHJE_SHOP,YHID,KH,CKH,PZH,LSH,SCANCODE,STATUS,GUID)
                Cursor cursor1_4 = sqldb.find("select ORDERNO,INX,SKTNO,JLBH,SKFS,JYSJ,LX,JYTYPE,SHDDH,WBDDH,SKJE,YHJE,YHJE_SHOP,YHID,KH,CKH,PZH,LSH,SCANCODE,STATUS,GUID " +
                        " from ORDER_PAYMENT_DETAIL where ORDERNO = " +"'"+ticket.getOrderNo()+"'"+" and SKTNO = "+"'"+ticket.getSktNo()+"'");
                if(cursor1_4 != null && cursor1_4.getCount() > 0){
                    while (cursor1_4.moveToNext()){
                        OrderPayitemOffline thirdPayItem = new OrderPayitemOffline();
                        thirdPayItem.setPay_code(CursorUtil.getString(cursor1_4,"SKFS"));
                        thirdPayItem.setPay_name(payItem.getPayName());
                        thirdPayItem.setPay_time(CursorUtil.getString(cursor1_4,"JYSJ"));
                        thirdPayItem.setPay_type(CursorUtil.getString(cursor1_4,"JYTYPE"));
                        thirdPayItem.setType(CursorUtil.getString(cursor1_4,"LX"));
                        thirdPayItem.setPay_amount(CursorUtil.getString(cursor1_4,"SKJE"));
                        thirdPayItem.setPay_yhje(CursorUtil.getString(cursor1_4,"YHJE"));
                        thirdPayItem.setPay_yhje_shop(CursorUtil.getString(cursor1_4,"YHJE_SHOP"));
                        thirdPayItem.setPay_cardNo(CursorUtil.getString(cursor1_4,"KH"));
                        thirdPayItem.setPay_ckh(CursorUtil.getString(cursor1_4,"CKH"));
                        thirdPayItem.setPay_pzh(CursorUtil.getString(cursor1_4,"PZH"));
                        thirdPayItem.setPay_lsh(CursorUtil.getString(cursor1_4,"LSH"));
                        thirdPayItem.setPay_guid(CursorUtil.getString(cursor1_4,"GUID"));
                        payItem.getPayInfoList().add(thirdPayItem);
                    }
                }
                cursor1_4.close();

                ticket.getPayList().add(payItem);

            }
        }

        cursor1.close();
    }

    private static void queryGoodsInfo(Sqldb sqldb, TicketOffline ticket) {
        int Discount=0;
        Cursor cursor1 = sqldb.find("select ORDERNO,SKTNO,JLBH,TCKT_INX,INX,YYY,DEPTID,DEPTCODE,SP_ID," +
                " BARCODE,CODE,LSDJ,XSSL,XSJE,ZKJE,YHJE,CRMINX,MBYH,GUID,INPUTCODE,TCKT_INX_OLD,INX_OLD " +
                " from ORDER_ITEM where ORDERNO = " +"'"+ticket.getOrderNo()+"'"+" and SKTNO = "+"'"+ticket.getSktNo()+"'");
        if(cursor1 != null && cursor1.getCount() > 0){
            while (cursor1.moveToNext()){
                GoodOffline goodItem = new GoodOffline();
                goodItem.setSubTicktInx(CursorUtil.getInt(cursor1, "TCKT_INX"));
                goodItem.setSubGoodsInx(CursorUtil.getInt(cursor1, "INX"));
                goodItem.setCrmInx(CursorUtil.getInt(cursor1, "CRMINX"));
                goodItem.setYyyPersonId(CursorUtil.getInt(cursor1, "YYY"));
                goodItem.setDeptId(CursorUtil.getInt(cursor1, "DEPTID"));
                goodItem.setDeptCode(CursorUtil.getString(cursor1, "DEPTCODE"));
                goodItem.setId(CursorUtil.getInt(cursor1, "SP_ID"));
                goodItem.setBarCode(CursorUtil.getString(cursor1, "BARCODE"));
                goodItem.setCode(CursorUtil.getString(cursor1, "CODE"));
                goodItem.setPrice(MoneyUtils.getMoneyInt(CursorUtil.getString(cursor1, "LSDJ")));
                goodItem.setSaleCount(CursorUtil.getDouble(cursor1, "XSSL"));
                goodItem.setSaleMoney(MoneyUtils.getMoneyInt(CursorUtil.getString(cursor1, "XSJE")));
                goodItem.setDiscount(MoneyUtils.getMoneyInt(CursorUtil.getString(cursor1, "ZKJE")));
                goodItem.setFrontDiscount(MoneyUtils.getMoneyInt(CursorUtil.getString(cursor1, "ZKJE")));
                goodItem.setPreferentialMoney(MoneyUtils.getMoneyInt(CursorUtil.getString(cursor1, "YHJE")));
                goodItem.setDecreaseDiscount(MoneyUtils.getMoneyInt(CursorUtil.getString(cursor1, "MBYH")));
                goodItem.setGoodsGuid(CursorUtil.getString(cursor1, "GUID"));
                goodItem.setInputCode(CursorUtil.getString(cursor1, "INPUTCODE"));
                goodItem.setSubTicktInx_old(CursorUtil.getInt(cursor1,"TCKT_INX_OLD"));
                goodItem.setSubGoodsInx_old(CursorUtil.getInt(cursor1,"INX_OLD"));

                //查询——SPXX
                Cursor cursor1_1 = sqldb.find("select SP_ID,NAME,SPFL,PACKED,SPTYPE,HSFS,STATUS"  +
                        " from SPXX where SP_ID = " +goodItem.getId()+" and BARCODE = "+"'"+goodItem.getBarCode()+"'");
                if(cursor1_1 != null && cursor1_1.getCount() > 0){
                    while (cursor1_1.moveToNext()){
                        goodItem.setName(CursorUtil.getString(cursor1_1,"NAME"));
                        goodItem.setGoodsType(CursorUtil.getInt(cursor1_1,"SPFL"));
                        goodItem.setClassType(CursorUtil.getString(cursor1_1,"SPTYPE"));
                        goodItem.setiHsfs(CursorUtil.getInt(cursor1_1,"HSFS"));
                        goodItem.setStatus(CursorUtil.getInt(cursor1_1,"STATUS"));
                        goodItem.setPackaged(CursorUtil.getInt(cursor1_1,"PACKED")==0?false:true);
                    }
                }
                cursor1_1.close();
                ticket.getGoodsList().add(goodItem);
                Discount+=goodItem.getDiscount();
            }
        }
        cursor1.close();
        ticket.setTotalDiscount(Discount);
    }

    public static void UpdateOfflineTicketStatus(Sqldb sqldb, String orderno ){
        try {
            //查询有多少条脱机数据
            int fscs = 0;
            Cursor cursor1 = sqldb.find("select FSCS from POS_TJSC where BJ = 0 and ORDERNO ="+"'"+orderno+"'");
            if(cursor1 != null && cursor1.getCount() > 0){
                while (cursor1.moveToNext()) {
                   fscs = CursorUtil.getInt(cursor1, "FSCS");
                }
                cursor1.close();
            }

            ContentValues values = new ContentValues();
            values.put("BJ", 1);
            values.put("FSCS", fscs+1);
            sqldb.update("POS_TJSC", values, "ORDERNO=?", new String[]{orderno+ ""});
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void UpdateOfflineTicketUploadNum(Sqldb sqldb, String orderno ){
        try {
            //查询有多少条脱机数据
            int fscs = 0;
            Cursor cursor1 = sqldb.find("select FSCS from POS_TJSC where BJ = 0 and ORDERNO ="+"'"+orderno+"'");
            if(cursor1 != null && cursor1.getCount() > 0){
                while (cursor1.moveToNext()) {
                    fscs = CursorUtil.getInt(cursor1, "FSCS");
                }
                cursor1.close();
            }
            ContentValues values = new ContentValues();
            values.put("FSCS", fscs+1);
            sqldb.update("POS_TJSC", values, "ORDERNO=?", new String[]{orderno+ ""});
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
